<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License */

class b2c_ctl_site_proInfo extends b2c_frontpage{

    function __construct($app){
        parent::__construct($app);
        $shopname = app::get('site')->getConf('site.name');
        $this->shopname = $shopname;
        $this->set_tmpl('proInfo');
        $this->_response->set_header('Cache-Control', 'no-store');
        $this->verify_member();
        $this->goodsModel= app::get('b2c')->model('goods');
        if(isset($shopname)){
            $this->title = app::get('b2c')->_('产品信息').'_'.$shopname;
            $this->keywords = app::get('b2c')->_('产品信息').'_'.$shopname;
            $this->description = app::get('b2c')->_('产品信息').'_'.$shopname;
        }

    }

    public function index(){
    	@ini_set('memory_limit','512M');
    	set_time_limit(0);
        $this->set_tmpl('proInfo');
        $this->path[] = array('title'=>app::get('b2c')->_('产品信息'),'link'=>$this->gen_url(array('app'=>'b2c', 'ctl'=>'site_proInfo', 'act'=>'index','full'=>1)));
        $GLOBALS['runtime']['path'] = $this->path;

        /** 得到商品的全部分类 **/
        $goodsObject = kernel::single('b2c_goods_object');
        $o_cat = $this->app->model('goods_cat');
        $o_goods = $this->app->model('goods');
        $arr_cats = $o_cat->getList('cat_id,cat_name,p_order',array('parent_id'=>'0'),0,-1,'p_order ASC');
        foreach ($arr_cats as $key=>&$cat){
            if (in_array($cat['cat_name'], array('经典面料分类推荐','职业装(工装)系列推荐','休闲时尚面料分类推荐'))) {
                unset($arr_cats[$key]);
            }
            $arr_sub_cats = $this->get_subcat_list($cat['cat_id']);

            // 查找二级分类下面的产品
            foreach ($arr_sub_cats as $k => &$v) {
                if(!$v['cat_id']) continue;
                $cat['cat_ids'][]=$v['cat_id'];
            }
            // $cat['sub_cats'] = $arr_sub_cats;
        }
        foreach ($arr_cats as $k=> &$vv) {
            if(!$vv['cat_ids']) continue;
            $arr_goods_o = $o_goods->getList('type_id,goods_id,cat_id,image_default_id,p_1,p_2,p_3,p_4,p_5,p_6,p_7,p_8,p_9,p_10,p_11,p_12,p_13,p_14,p_15,p_16,p_17,p_18,p_19,p_20,p_21,p_22,p_23,p_24,p_25,p_26,p_27,p_28,p_29,p_30,p_31,p_32,p_33,p_34,p_35,p_36,p_37,p_38,p_39,p_40,p_41,p_42,p_43,p_44,p_45,p_46,p_47,p_48,p_49,p_50',array('cat_id|in'=>$vv['cat_ids'],'marketable'=>'true'));
            foreach ($arr_goods_o as $kk => &$v1) {
                $parent_cat_name = $o_cat->getRow('*',array('cat_id'=>$v1['cat_id']));
                $v1['parent_cat_name'] = $parent_cat_name['cat_name'];
                $v1['parent_cat_order'] = $parent_cat_name['p_order'];


                //每个产品的扩展属性
                // $goodsBasic = $this->get_goods_type($v1['goods_id']);
                // $v1['prop'] = $this->_get_goods_props($goodsBasic['type']['props'],$v1);

                $result[$v1["cat_id"]]['parent_cat_order'] = $v1['parent_cat_order'];
                $result[$v1["cat_id"]][] = $v1;
                // unset($v1);
            }

            unset($vv['cat_ids']);
            $vv['res'] = $result;
            unset($result);
        }


        foreach ($arr_cats as $kk => &$v1) {
            foreach ($v1['res'] as &$v2) {
                $v2['num'] = count($v2)-1;
            }
            $v1['num']=$v2;
        }

        //对产品信息页分类进行排序
        $flag=array();
        foreach($arr_cats as &$arr2){
            foreach($arr2['res'] as &$arr3){
                $flag[]=$arr3["parent_cat_order"];
            }
            array_multisort($flag, SORT_ASC, $arr2['res']);
            unset($flag);
        }

        // dump($arr_cats);die;
        $this->pagedata['cats'] = $arr_cats;
        // $this->pagedata['goods'] = $arr_goods_o;

        // $this->page('site/proInfo/index.html');
        $this->page('site/proInfo/index.html');
    }

    // 三大分类下面的二级分类
    function get_subcat_list($cat_id){
        $o_cat = $this->app->model('goods_cat');
        $filter = array('parent_id'=>$cat_id);
        $list = $o_cat->getList('cat_id,p_order',$filter,0,-1,'p_order ASC');
        return $list;
    }

    function _get_goods_props($arrProps,$aGoods){

        if( empty($arrProps) ){
            return null;
        }
        $goodsProps = array();
        for ($i=1;$i<=50;$i++){
            //1-20 select 21-50 input
            if ($aGoods['p_'.$i] ){
                $propsValueId = $aGoods['p_'.$i];
                $k = $arrProps[$i]['ordernum_p'].'_'.$i;
                if( $i <= 20 ){
                    if($arrProps[$i]['show_p']=='on'){
                        $goodsProps[$k]['name'] = $arrProps[$i]['name'];
                        $goodsProps[$k]['alias'] = $arrProps[$i]['alias'];
                        $goodsProps[$k]['value']  =$arrProps[$i]['options'][$propsValueId];
                    }
                }else{
                    if($arrProps[$i]['show_p']=='on'){
                        $goodsProps[$k]['name'] = $arrProps[$i]['name'];
                        $goodsProps[$k]['alias'] = $arrProps[$i]['alias'];
                        $goodsProps[$k]['value'] = $propsValueId;
                    }
                }
            }
        }
        ksort($goodsProps);
        return $goodsProps;
    }

    public function get_goods_type($goodsId,$goodsInfo=array()){
        if( empty($goodsInfo) ){
            $goodsInfo = $this->goodsModel->getList( 'type_id', array('goods_id'=>$goodsId) );
            $goodsInfo = $goodsInfo[0];
        }
        $goodsInfoType= array();
        if(!empty($goodsInfo['type_id'])){
            $goodsInfoType = $this->dump2(array('type_id'=>$goodsInfo['type_id']) );
        }
        return $goodsInfoType;

    }

    function dump2($filter,$field = '*'){
        $rs = $this->goodsModel->getRow($field,$filter);
        if($rs){
            $propsData = app::get('b2c')->model('goods_type_props')->getList('*',array('type_id'=>$rs['type_id'],'show_p'=>'on'));
            $props = array();
            foreach((array)$propsData as $row){
                $props_ids[] = $row['props_id'];
                $props[$row['props_id']] = $row;
            }
            if($props_ids){
                //增加对扩展属性值的排序 2016年7月5日14:57:19 by shen
                $props_value_data = app::get('b2c')->model('goods_type_props_value')->getList('*',array('props_id'=>$props_ids),0,-1,'order_by asc');
            }
            foreach((array)$props_value_data as $row){
                $props[$row['props_id']]['props_value'][$row['props_value_id']] = $row;
            }
        }
        $rs['props'] = $props;

        $props = array();
        if( $rs['props'] ){
            foreach( $rs['props'] as $k => $v ){
                $props[$v['goods_p']] = $v;
                if( $v['props_value'] )
                    foreach( $v['props_value'] as $vk => $vv ){
                        $props[$v['goods_p']]['options'][$vv['props_value_id']] = $vv['name'];
                        $props[$v['goods_p']]['optionAlias'][$vv['props_value_id']] = $vv['alias'];
                        $props[$v['goods_p']]['optionIds'][$vv['props_value_id']] = $vv['props_value_id'];
                    }
                unset( $props[$v['goods_p']]['props_value'] );
            }
            unset( $rs['props'] );
            $rs['props'] = $props;
        }
        return $rs;
    }

    // ajax获取商品
    function ajaxGetgoods(){
        $cat_id = $_POST['cat_id'];
        $return = $this->detail_goods($cat_id);
        echo ($return);exit;
    }

    public function detail_goods($cat_id=null){
        if($cat_id){
            $re_goods = $this->goodsModel->getList('*',array('cat_id'=>$cat_id,'marketable'=>'true'),0,-1,'goods_id asc');
            //每个产品的扩展属性
            $goodsInfoType = $this->dump2(array('type_id'=>$re_goods[0]['type_id']) );
            // $goodsBasic = $this->get_goods_type($v['goods_id']);

            foreach ($re_goods as $k => &$v) {
                $productMode = app::get('b2c')->model('products');
                $productData = $productMode->getList('product_id,bn,price,cost,mktprice,store,freez,store_place,weight,marketable,spec_desc,is_default',array('goods_id'=>$v['goods_id']));
                $v['product_id'] = $productData[0]['product_id'];

                $product_ids[]=$v['bn'];
                //每个产品的扩展属性
                $v['prop'] = $this->_get_goods_props($goodsInfoType['props'],$v);
                $return[$v["cat_id"]][] = $v;
            }
            $p_ids=implode(',',$product_ids);

            //每个产品的库存
            $post = array(
                'method'=>'Cangku.getproductsKucun',
                'params'=>array(
                    'product_id'=>$p_ids,
                )
            );
            $erp_caller_obj = kernel::service('apioubao.rpc_erp_request');
            $re = $erp_caller_obj->call_api_oubao($post);
            $re = json_decode($re,true);
            if($re['rsp']){
                foreach ($return[$cat_id] as $key => &$value) {
                    foreach ($re['data'] as $kk => &$vv) {
                        if($vv['productId']==$value['bn']){
                            $value['store_jxc'] = round($vv['cnt'],2);
                        }
                    }
                }
            }
            $return['success'] = true;
        }
        else{
            $return['success'] = false;
        }
        $return = $return[$cat_id];
        $this->pagedata['return'] = $return;
        $this->pagedata['cat_id'] = $cat_id;
        echo $this->fetch('site/proInfo/goodsList.html');
    }

    /**
     * ps ：总加单信息表
     * Time :2016年8月4日15:45:4322
     * @author shen
     * @param 参数类型
     * @return 返回值类型
    */
    function allorderInfo($product_id = ''){
        $post = array(
            'method'=>'Cangku.productsZaitu',
            'params'=>array(
                'product_id'=>'all',
                'kuweiName'=>'现货',
            )
        );
        $erp_caller_obj = kernel::service('apioubao.rpc_erp_request');
        $result = $erp_caller_obj->call_api_oubao($post);
        $r = json_decode($result,true);
        // dump($r);die;
        $this->pagedata['row'] = $r['data'];
        $this->display('site/proInfo/allproNew.html');
    }

    /**
     * ps ：加单信息表
     * Time :2016年8月4日15:45:4322
     * @author shen
     * @param 参数类型
     * @return 返回值类型
    */
    function orderInfo($product_id = ''){
        $product = app::get('b2c')->model('products')->getRow('bn',array('product_id'=>$product_id));
        if(!$product){
            echo app::get('b2c')->_('商品不存在');
            exit;
        }
        $retProduct=app::get('b2c')->model('products')->getBnJxc($product['bn']);
        $post = array(
            'method'=>'Cangku.zaitu',
            'params'=>array(
                'product_id'=>$retProduct['bn'],
                'kuweiName'=>'现货',
            )
        );
        $erp_caller_obj = kernel::service('apioubao.rpc_erp_request');
        $result = $erp_caller_obj->call_api_oubao($post);
        $r = json_decode($result,true);
        // dump($r);die;
        $this->pagedata['row'] = $r['data'];
        $this->pagedata['product_id']=$product_id;
        $this->display('site/proInfo/pro-New.html');
    }

}

